package com.pk.platform.business.question.dao.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.pk.platform.business.core.dao.impl.GenericDaoImpl;
import com.pk.platform.business.question.dao.IPaperUseDao;
import com.pk.platform.business.question.vo.PaperVO;
import com.pk.platform.domain.question.Question;
import com.pk.platform.util.StringConverter;
import com.pk.platform.util.page.Pager;

public class PaperUseDaoImpl extends GenericDaoImpl implements IPaperUseDao {

	public List<Map<String, Object>> queryPaperFixByPage(PaperVO pvo, Pager pager)
	{
		StringBuffer sql = new StringBuffer();
		List<Object> param = new ArrayList<Object>();
		param.add(pvo.getPaperId());
		sql.append("select pf.* from paper_fix pf where 1 = 1 and pf.paperId = ?");
		if(StringConverter.isNotNull(pvo.getPfCode()))
		{
			sql.append(" and pf.pfCode like ?");
			param.add("%" + pvo.getPfCode() + "%");
		}
		if(StringConverter.isNotNull(pvo.getPfName()))
		{
			sql.append(" and pf.pfName like ?");
			param.add("%" + pvo.getPfName() + "%");
		}
		if(StringConverter.isNotNull(pvo.getPfSex()))
		{
			sql.append(" and pf.pfSex = ?");
			param.add(pvo.getPfSex());
		}
		sql.append(" order by pf.pfCode");
		return this.getListByPage(sql.toString(), param.toArray(), pager);
	}
	
	public boolean existPaperFixCode(String code, String paperId)
	{
		String sql = "select count(0) from paper_fix pf where pf.pfCode = ? and pf.paperId = ?";
		int count = this.getSjt().queryForInt(sql, code, paperId);
		if(count > 0)
		{
			return true;
		}
		return false;
	}
	
	public List<Map<String, Object>> queryQuestionListByPaper(String paperId)
	{
		StringBuffer sql = new StringBuffer();
		sql.append("select q.* from question q where q.paperId = ? order by q.page, q.qesCode");
		return this.getSjt().queryForList(sql.toString(), new Object[]{paperId});
	}
	
	public List<Map<String, Object>> queryQuestionItemOption(String ctId)
	{
		StringBuffer sql = new StringBuffer();
		sql.append("select io.* from item_option io where io.ctId = ? order by io.optionValue");
		return this.getSjt().queryForList(sql.toString(), ctId);
	}
	
	public Map<String, Object> queryOneCodeTable(String ctId)
	{
		String sql = "select * from code_table ct where ct.id = ?";
		List<Map<String, Object>> list = this.getSjt().queryForList(sql, new Object[]{ctId});
		if(list.size() > 0)
			return list.get(0);
		return null;
	}
	
	public List<Map<String, Object>> queryQuestionKeyList(String pfId)
	{
		String sql = "select qk.* from question_key qk where qk.pfId = ?";
		return this.getSjt().queryForList(sql, pfId);
	}
	
	public void delQuestionKeyByPaperFix(String pfId)
	{
		String sql = "delete from question_key where pfId = ?";
		this.getSjt().update(sql, pfId);
	}
	
}
